﻿@{
    Layout = "~/Views/Shared/Index_Layout.cshtml";
}


<table class="layui-hide" id="standardtable" lay-filter="standardtable"></table>


<script type="text/html" id="toolbar">
    <div class="layui-btn-container">

          <a class="common_btn" href="/SysRole/Edit">添加</a>

    </div>
</script>

<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="delete">删除</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="authorization">授权</a>
</script>

<!-- 授权 树 -->
<div class="layui-dialog authorization-dialog" style="display: none;">
    <form class="layui-form" action="" id="form" lay-filter="">
        <div class="content_wrap">


            <div id="authorizationTree">
            </div>
            <div class="bottom_btn" style="padding: 0.1rem 0 0.25rem;">

                <button lay-submit class="common_btn" id="save" type="button">保存</button>
                <div class="common_btn" id="close">取消</div>

            </div>
        </div>
    </form>
</div>

<script>

    var treestr = '默认'; //树节点 id
    var roleid = ''; //当前的角色 id
    var layerIndex;

    var childArray = null;
    var selectedType = null; //父界获取此值，来判断是单选还是多选。参考值：多选，单选

    layui.use(function () {

        var $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            GSLayui = layui.GSLayui;
        var tree = layui.tree;

        var cols = [

            { title: "序号", width: 60, templet: function (d) { return d.LAY_INDEX; } },
            { field: "Name", title: "名称 ", align: "center" },
            { field: "Description", title: "描述", align: "center" },
            { field: "CreatedTime", title: "创建时间", align: "center" },
            { title: '操作', minWidth: 190, toolbar: '#currentTableBar', align: "center" }


        ];

        //注意：parent 是 JS 自带的全局对象，可用于操作父页面
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        if (window.name) {
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            if (index) {


                var iframe = window['layui-layer-iframe' + index];
                //在弹框中操作
                $("#toolbar").html('<a id="selectValue" class="common_btn">选择</a>');
                //增加一个选择列
                if (parent.parentSelectedType == '多选') {//多选
                    cols.unshift({ type: "checkbox", width: 50 });
                }
                else {//单选
                    cols.unshift({ type: "radio", width: 50 });
                }
                //删除最后一个操作列
                cols.pop();
                //从父页面读取之前选中的数据，默认选中
                selectedType = parent.selectedType;
                childArray = parent.parentArray;

            }

        } else {
            //不在弹框中操作

        }

        var standardtable = GSLayui.layuiTable("standardtable", "/SysRole/DataSysRole", cols)

        standardtable.done = function (obj) {//数据渲染完毕的回调函数
             
            //回显
             
            // 回显选中的节点
            if (childArray) {
                var tempData = obj.data;
                tempData.map(function (item, index) {
                   
                    if (childArray)
                        childArray.map(function (items, indexs) {
                            if (tempData[index].Id == items.Id) {
                                
                                // 勾选或取消勾选单个节点
                                table.setRowChecked('standardtable', {
                                    index: index,
                                     checked: true // 选中
                                 });                                

                            }
                        })

                })

            }
        };

        table.render(standardtable);

       
        //监听表格按钮
        table.on('tool(standardtable)', function (obj) {
            switch (obj.event) {

                case 'edit':

                    window.location.href = "/SysRole/Edit?id=" + obj.data.Id;
                    break;
                case 'authorization':
                    //弹框
                    layerIndex = layer.open({
                        type: 1,
                        title: "授权",
                        area: ["600px", "500px"],
                        content: $(".authorization-dialog"),
                        success: function (layero, index) {

                            var data = ajaxGet("/SysRole/SystemTree/" + obj.data.Id);
                            if (data.code == 200) {

                                roleid = obj.data.Id;
                                data = data.data;

                                tree.render({
                                    elem: "#authorizationTree",
                                    id: "treeId",
                                    data: data,
                                    showCheckbox: true,
                                    oncheck: function (obj) {

                                        //拿到保存时取数据

                                    }

                                });

                            } else {
                                layer.alert(data.msg, {
                                    title: "失败"
                                });
                            }


                        }
                    });

                    break;

                case 'delete':
                    layer.confirm('您确认删除？', function (index) {

                        var data = ajaxPost("/SysRole/DeleteSysRole/" + obj.data.Id);
                        if (data.code == 200) {
                            layer.alert(data.msg, {
                                title: "成功"
                            });

                            setTimeout(function () {
                                layer.close(layer.index); //自动关闭
                            }, 2000);

                            table.reload('standardtable');

                        } else {
                            layer.alert(data.msg, {
                                title: "失败"
                            });
                        }
                    });
                    break;

            };
        });
        //选择按钮
        $('#selectValue').click(function () {
            debugger
            var checkStatus = table.checkStatus('standardtable');
            parent.selectValue(checkStatus.data);
         
            parent.layer.close(index);

        });

        $('body').on('click', "#save", function () {

            res = tree.getChecked('treeId');

            console.log(res);//当前树的数据
            treestr = '';
            getAllChildrenNodes(res);
            treestr = treestr.substring(1);
            console.log('处理后数据');
            console.log(treestr);


            if (treestr == "默认") //没用做任何操作
            {

                layer.close(layerIndex);

                return;

            } else {
                var data = ajaxPost("/SysRole/SetMenu/" + roleid, { SysMenuId: treestr, SysRoleId: roleid });

                if (data.code == 200) {
                    layer.confirm(data.msg, {
                        title: "信息"
                    }, function (index) {

                        layer.close(index);
                        layer.close(layerIndex);
                    });



                    return;
                } else {
                    layer.alert(data.msg, {
                        title: "失败"
                    });
                    IsLoading = false;//禁止重复提交
                }
            }

        })
        $('body').on('click', "#close", function () {
            
            layer.close(layerIndex)
        })
        function getAllChildrenNodes(data) {
            for (let i = 0; i < data.length; i++) {
                if (data[i].children) {
                    //treestr = treestr + "," + data[i].id;
                    getAllChildrenNodes(data[i].children)
                } else {
                    if (data[i]) {
                        treestr = treestr + "," + data[i].id;
                    }
                }
            }
            console.log(treestr.substring(1))
        }

    });




</script>